package cn.aimee.oss.ui.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.util.UUID;

import androidx.annotation.Nullable;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.aimee.oss.Config;
import cn.aimee.oss.OSSManage;
import cn.aimee.oss.R;
import cn.aimee.oss.util.AbDateUtil;
import cn.aimee.oss.util.FileUtil;
import cn.aimee.permission.ui.activity.PermissionBaseActivity;
import cn.aimee.permission.util.permission.PermissionDescriptionUtil;

public class MainActivity extends PermissionBaseActivity {

    private static final int REQUEST_CODE_CHOOSE_PIC = 0x100;

    private OSSManage ossManage;
    private String localPicPath;
    private String objectName;//图片Object完整路径，不包含Bucket名称

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        init();
    }

    @OnClick({R.id.btn_choose_pic, R.id.btn_upload_pic, R.id.btn_delete_pic})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_choose_pic:
                choosePic();
                break;
            case R.id.btn_upload_pic:
                uploadPic();
                break;
            case R.id.btn_delete_pic:
                deletePic();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.i("zhihua", "获取到的图片uri: " + data.getData().getPath());
        localPicPath = FileUtil.getFilePathByUri(MainActivity.this, data.getData());
        Log.i("zhihua", "获取到的图片path: " + localPicPath);
    }

    private void init() {
        checkPermissions(PermissionDescriptionUtil.storagePermissions, new OnPermissionResultListener() {
            @Override
            public void onPermissionGranted() {

            }

            @Override
            public void onPermissionDenied() {

            }
        });
    }

    private void choosePic() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            startActivityForResult(new Intent(Intent.ACTION_GET_CONTENT).setType("image/*"), REQUEST_CODE_CHOOSE_PIC);
        } else {
            Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setType("image/*");
            startActivityForResult(intent, REQUEST_CODE_CHOOSE_PIC);
        }
    }

    private void uploadPic() {
        if (null == ossManage) {
            ossManage = new OSSManage(getApplicationContext());
        }
        objectName = Config.OBJECT_NAME + "/" + AbDateUtil.getCurDateStr(AbDateUtil.dateFormatYMD) + "/"+ UUID.randomUUID().toString() + ".png";
        ossManage.upload(Config.BUCKET_NAME, objectName, localPicPath);
    }

    private void deletePic() {
        ossManage.deleteFile(objectName);
    }

}